from celery_task import app
from django.conf import settings
from django.core.cache import cache

from home.models import Banner
from home.serializer import BannerModelSerializer
from utils import time


@app.task
def update_banner():
    """
     [
        OrderedDict([('name', 'one'), ('link', 'free-cours'), ('img', '/media/banner/banner1_HED5xR8.png')]),
        OrderedDict([('name', '22'), ('link', 'actual-course'), ('img', '/media/banner/banner2_RjMwOq3.png')]),
        OrderedDict([('name', '33'), ('link', 'light-course'), ('img', '/media/banner/banner3_1yVb7K2.png')])
    ]
     [
        OrderedDict([('name', 'one'), ('link', 'free-cours'), ('img', 'http://127.0.0.1:8088/media/banner/banner1.png')]),
        OrderedDict([('name', '22'), ('link', 'actual-course'), ('img', 'http://127.0.0.1:8088/media/banner/banner2.png')]),
        OrderedDict([('name', '33'), ('link', 'light-course'), ('img', 'http://127.0.0.1:8088/media/banner/banner3.png')])
    ]
    """
    banner_instance = Banner.objects.filter(is_show=True, is_delete=False).order_by('orders')[:settings.BANNER_COUNTER]
    banner_serializer = BannerModelSerializer(instance=banner_instance, many=True)
    banner_serializer_data = banner_serializer.data
    for order_dict in banner_serializer_data:
        order_dict['img'] = settings.HTTP_PREFIX + order_dict['img']
    # print(banner_serializer_data)
    cache.set(settings.BANNER_KEY, banner_serializer_data, time())
    return True
